//
//  LRXBedRoomView.m
//  路人侠
//
//  Created by zhangxulong on 5/7/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXBedRoomView.h"
@interface LRXBedRoomView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,weak) LRXBedroom * bedroom;

@property (nonatomic,strong) NSMutableArray * bedRoomNums;
@property (nonatomic,strong) NSMutableArray *  floorNums;
@property (nonatomic,weak) UIPickerView * pick;
@property (nonatomic,copy) NSString * floorNumStr;
@property (nonatomic,copy) NSString * bedRoomNumStr;
@end
@implementation LRXBedRoomView
- (NSMutableArray *)bedRoomNums
{
    if (_bedRoomNums == nil) {
        _bedRoomNums = [NSMutableArray array];
    }
    return _bedRoomNums;
}

- (void)reloadNewData:(LRXBedroom *)bedroom
{
    self.bedroom = bedroom;
    [self handleData:bedroom];
    
    
    [self.pick selectRow:0 inComponent:0 animated:YES];
    [self.pick reloadAllComponents];
}
- (void)layoutBedRoom:(LRXBedroom *)bedroom
{
    
    self.bedroom = bedroom;
    self.backgroundColor = [UIColor whiteColor];
    self.floorNumStr = @"";
    self.bedRoomNumStr = @"";
    
    UIPickerView * pick = [[UIPickerView alloc]init];
    CGFloat pickX = 150 * wScale;
    CGFloat pickY = 50 * hScale;
    CGFloat pickW = MainScreenW - 2 * pickX;
    CGFloat pickH = self.frame.size.height - 2 * pickY;
    pick.frame = CGRectMake(pickX, pickY, pickW , pickH);
    pick.delegate = self;
    pick.dataSource = self;
    self.pick = pick;
    [self handleData:bedroom];
    [self addSubview:pick];
    
}
- (void)handleData:(LRXBedroom *)bedroom
{
    int floor[10] = {0};
    for(NSString * str in bedroom.bedroomNums)
    {
        NSString * subStr = [str substringToIndex:1];
        NSInteger p = [subStr integerValue];
        floor[p] = 1;

    }
    NSMutableArray * floorNum = [NSMutableArray array];
    for(int i = 0; i < 10; i++)
    {
        if (floor[i] == 1) {
            [floorNum addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    
    NSMutableArray * bedRoomNums = [NSMutableArray array];
    for (NSString * str in self.bedroom.bedroomNums) {
        if ([str hasPrefix:floorNum[0]]) {
            [bedRoomNums addObject:str];
        }
    }
    self.bedRoomNums = bedRoomNums;
    
    
//？？ 如果用self.floorNums 弱引用 就不行
    self.floorNums = floorNum;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.floorNums.count;
    }
    else
    {
        return self.bedRoomNums.count;
    }
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel * pickLabel = [[UILabel alloc]init];
    pickLabel.textColor = mainColor;
    pickLabel.font = [UIFont systemFontOfSize:28 * hScale];
    pickLabel.textAlignment = NSTextAlignmentCenter;
    
    if (component == 0) {
        pickLabel.text = [self.floorNums[row] stringByAppendingString:@"层"];
    }
    else
    {
        pickLabel.text = [self.bedRoomNums[row] stringByAppendingString:@"号"];
    }
    
    
    [pickLabel sizeToFit];
    return pickLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    UILabel * label = (UILabel *)[pickerView viewForRow:row forComponent:component];
    label.textColor = orangeColor;
    if (component == 0) {
        NSMutableArray * bedRoomNums = [NSMutableArray array];
        
        for (NSString * str in self.bedroom.bedroomNums) {
            
            if ([str hasPrefix:self.floorNums[row]]) {
                
                [bedRoomNums addObject:str];
            }
        }
        self.bedRoomNums = bedRoomNums;
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }
    if (component == 0) {
        self.floorNumStr = [self.floorNums[row] stringByAppendingString:@"层"];
        
    }
    else
    {
        self.bedRoomNumStr = [self.bedRoomNums[row] stringByAppendingString:@"号"];
    }
    
    if ([self.delegate respondsToSelector:@selector(bedRoomSelectWithFloorNum:withBlockNum:)]) {
        [self.delegate bedRoomSelectWithFloorNum:self.floorNumStr withBlockNum:self.bedRoomNumStr];
    }
}

@end
